import { _decorator, Component, instantiate, Label, Node, Prefab, v3 } from 'cc';
import { Cow } from './ctrl/Cow';
import { AudioManager } from '../../framework/manager/AudioManager';
import { Rope } from './ctrl/Rope';
import { ResourceMgr } from '../../framework/manager/ResourceMgr';
const { ccclass, property } = _decorator;

@ccclass('CowGame')
export class CowGame extends Component {

    private isStarted: boolean

    private score: number

    private scoreLabel: Label

    private time: number

    private timeLabel: Label

    private cowSet: Node[]

    private resuleBox: Node

    private resultScoreLabel: Label

    private resultTitleLabel: Label;

    private shootBtn: Node

    @property(Prefab)
    private cowPrefab: Prefab

    protected onLoad(): void {
        this.isStarted = false;
        this.cowSet = []

        this.score = 0
        this.scoreLabel = this.node.getChildByPath("UIRoot/Score/Label").getComponent(Label)

        this.time = 60
        this.timeLabel = this.node.getChildByPath("UIRoot/Time/Label").getComponent(Label)

        this.resuleBox = this.node.getChildByPath("UIRoot/ResultBox")
        this.resultScoreLabel = this.resuleBox.getChildByName("Score").getComponent(Label)
        this.resultTitleLabel = this.resuleBox.getChildByName("Title").getComponent(Label)
       
    }

    protected start(): void {
        let rope = this.node.getChildByName("Rope").getComponent(Rope)
        rope.init(this)


        this.shootBtn = this.node.getChildByName("ShootBtn")
        ResourceMgr.Instance.addButtonListen(this.shootBtn, this, () => {
            console.info('rope shoot')
            rope.shoot()
        })

        this.startGame()
    }

    private startGame() {
        if (this.isStarted) {
            return;
        }
        this.isStarted = true;
        this.resuleBox.active = false;
        this.score = 0;
        this.scoreLabel.string = "0"

        this.time = 60
        this.timeLabel.string = "60"

        this.unscheduleAllCallbacks()
        this.scheduleOnce(this.onTimer.bind(this), 1)
        this.genCow()
    }

    private onTimer() {
        this.time--
        this.timeLabel.string = "" + this.time

        if (this.time <= 0) {
            this.unscheduleAllCallbacks()
            this.isStarted = false;
            this.checkout()
            return;
        }
        this.scheduleOnce(this.onTimer.bind(this), 1)
    }

    private checkout() {
        this.resuleBox.active = true;
        this.resultScoreLabel.string = "" + this.score

        var tital_set = ["情场空手", "情场新手", "情场高手", "情场大师"];
        if (this.score <= 0) {
            this.resultTitleLabel.string = tital_set[0];
        }
        else if (this.score <= 3) {
            this.resultTitleLabel.string = tital_set[1];
        }
        else if (this.score <= 6) {
            this.resultTitleLabel.string = tital_set[2];
        }
        else {
            this.resultTitleLabel.string = tital_set[3];
        }
    }

    removeCow(cowNode: Node) {
        let idx = this.cowSet.indexOf(cowNode)
        this.cowSet.splice(idx, 1)
    }

    hitCow(): number {
        for (let i = 0; i < this.cowSet.length; i++) {
            let cowNode = this.cowSet[i]
            let cowPos = cowNode.getPosition()
            if (cowPos.x >= 70 && cowPos.x <= 130) {
                let cowIdx = cowNode.getComponent(Cow).typeIdx
                cowNode.removeFromParent()
                this.cowSet.splice(i, 1)

                this.score++
                this.scoreLabel.string = "" + this.score
                AudioManager.Instance.playAudio("cowboy/sounds/niu")
                return cowIdx;
            }
        }
        return -1;
    }

    genCow() {
        let cow = instantiate(this.cowPrefab)
        cow.getComponent(Cow).init(this)
        this.node.addChild(cow)
        cow.active = true;
        cow.setPosition(v3(640, -100, 0))
        this.cowSet.push(cow)
        this.scheduleOnce(this.genCow.bind(this), Math.random() * 3 + 4);
    }
}

